{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "cellular-bathroom",
   "metadata": {},
   "source": [
    "# Problem 14.07 Liquid Nitrogen Production Via Volume Expansion of the Compressed Gas"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "regional-depression",
   "metadata": {},
   "source": [
    "Nitrogen at -104 °C and 250 bar flows through a valve to a pressure of 1 bar. What fraction of the stream becomes liquid?"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "greek-costume",
   "metadata": {},
   "source": [
    "## Solution\n",
    "\n",
    "This is straightforward; energy is conserved and outlet presure is specified, making this a PH flash. This problem is also an important application that can show the results of different equations of state and how important good thermodynamics are. \n",
    "\n",
    "We can compare many different EOSs with Thermo easily."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "decreased-milwaukee",
   "metadata": {},
   "outputs": [],
   "source": [
    "from thermo import *\n",
    "from thermo.interaction_parameters import SPDB\n",
    "fluid = 'nitrogen'\n",
    "constants, correlations = ChemicalConstantsPackage.from_IDs([fluid])\n",
    "\n",
    "T1 = -104 + 273.15\n",
    "P1 = 240*1e5\n",
    "zs = [1]\n",
    "P2 = 1e5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "electric-curtis",
   "metadata": {},
   "outputs": [],
   "source": [
    "flasher_objects = []\n",
    "flasher_names = []\n",
    "\n",
    "gas = CoolPropGas('HEOS', fluid, T=T1, P=P1, zs=zs)\n",
    "liquid = CoolPropLiquid('HEOS', fluid, T=T1, P=P1, zs=zs)\n",
    "high_precision = FlashPureVLS(constants, correlations, gas=gas, liquids=[liquid], solids=[])\n",
    "flasher_objects.append(high_precision)\n",
    "flasher_names.append('High-Precision')\n",
    "\n",
    "# Add the Peng-Robinson Pina-Martinez parameters EOS\n",
    "Ls = SPDB.get_parameter_vector(name='PRTwu_PinaMartinez', CASs=constants.CASs, parameter='TwuPRL')\n",
    "Ms = SPDB.get_parameter_vector(name='PRTwu_PinaMartinez', CASs=constants.CASs, parameter='TwuPRM')\n",
    "Ns = SPDB.get_parameter_vector(name='PRTwu_PinaMartinez', CASs=constants.CASs, parameter='TwuPRN')\n",
    "cs = SPDB.get_parameter_vector(name='PRTwu_PinaMartinez', CASs=constants.CASs, parameter='TwuPRc')\n",
    "alpha_coeffs = [(Ls[i], Ms[i], Ns[i]) for i in range(constants.N)]\n",
    "eos_kwargs = {'Pcs': constants.Pcs, 'Tcs': constants.Tcs, 'omegas': constants.omegas,\n",
    "'cs': cs, 'alpha_coeffs':alpha_coeffs}\n",
    "\n",
    "gas = CEOSGas(PRMIXTranslatedConsistent, eos_kwargs=eos_kwargs, HeatCapacityGases=correlations.HeatCapacityGases)\n",
    "liquid = CEOSLiquid(PRMIXTranslatedConsistent, eos_kwargs=eos_kwargs, HeatCapacityGases=correlations.HeatCapacityGases)\n",
    "eos_obj = FlashPureVLS(constants, correlations, gas=gas, liquids=[liquid], solids=[])\n",
    "flasher_objects.append(eos_obj)\n",
    "flasher_names.append('PR-Pina-Martinez')\n",
    "\n",
    "# Add the SRK Pina-Martinez parameters EOS\n",
    "Ls = SPDB.get_parameter_vector(name='SRKTwu_PinaMartinez', CASs=constants.CASs, parameter='TwuSRKL')\n",
    "Ms = SPDB.get_parameter_vector(name='SRKTwu_PinaMartinez', CASs=constants.CASs, parameter='TwuSRKM')\n",
    "Ns = SPDB.get_parameter_vector(name='SRKTwu_PinaMartinez', CASs=constants.CASs, parameter='TwuSRKN')\n",
    "cs = SPDB.get_parameter_vector(name='SRKTwu_PinaMartinez', CASs=constants.CASs, parameter='TwuSRKc')\n",
    "alpha_coeffs = [(Ls[i], Ms[i], Ns[i]) for i in range(constants.N)]\n",
    "eos_kwargs = {'Pcs': constants.Pcs, 'Tcs': constants.Tcs, 'omegas': constants.omegas,\n",
    "'cs': cs, 'alpha_coeffs':alpha_coeffs}\n",
    "\n",
    "gas = CEOSGas(SRKMIXTranslatedConsistent, eos_kwargs=eos_kwargs, HeatCapacityGases=correlations.HeatCapacityGases)\n",
    "liquid = CEOSLiquid(SRKMIXTranslatedConsistent, eos_kwargs=eos_kwargs, HeatCapacityGases=correlations.HeatCapacityGases)\n",
    "eos_obj = FlashPureVLS(constants, correlations, gas=gas, liquids=[liquid], solids=[])\n",
    "flasher_objects.append(eos_obj)\n",
    "flasher_names.append('SRK-Pina-Martinez')\n",
    "\n",
    "# Add a bunch of EOSs that don't require any parameters\n",
    "eos_kwargs = dict(Tcs=constants.Tcs, Pcs=constants.Pcs, omegas=constants.omegas)\n",
    "\n",
    "cubic_EOSs = [('PR', PRMIX), ('SRK', SRKMIX), \n",
    "              ('VDW', VDWMIX),\n",
    "             ('PRSV', PRSVMIX), ('PRSV2', PRSV2MIX),\n",
    "             ('TWUPR', TWUPRMIX), ('TWUSRK', TWUSRKMIX),\n",
    "             ('PRTranslatedConsistent', PRMIXTranslatedConsistent),\n",
    "             ('SRKTranslatedConsistent', SRKMIXTranslatedConsistent)]\n",
    "for eos_name, eos_obj in cubic_EOSs:\n",
    "    liquid = CEOSLiquid(eos_obj, HeatCapacityGases=correlations.HeatCapacityGases, eos_kwargs=eos_kwargs)\n",
    "    gas = CEOSGas(eos_obj, HeatCapacityGases=correlations.HeatCapacityGases, eos_kwargs=eos_kwargs)\n",
    "    eos_obj = FlashPureVLS(constants, correlations, gas=gas, liquids=[liquid], solids=[])\n",
    "\n",
    "    flasher_objects.append(eos_obj)\n",
    "    flasher_names.append(eos_name)\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "realistic-harvest",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The High-Precision EOS predicted liquid molar fraction is 0.03887228.\n",
      "The PR-Pina-Martinez EOS predicted liquid molar fraction is 0.05514570.\n",
      "The SRK-Pina-Martinez EOS predicted liquid molar fraction is 0.06744189.\n",
      "The PR EOS predicted liquid molar fraction is 0.05852484.\n",
      "The SRK EOS predicted liquid molar fraction is 0.04234207.\n",
      "The VDW EOS predicted liquid molar fraction is 0.00000000.\n",
      "The PRSV EOS predicted liquid molar fraction is 0.05903916.\n",
      "The PRSV2 EOS predicted liquid molar fraction is 0.05903916.\n",
      "The TWUPR EOS predicted liquid molar fraction is 0.05437623.\n",
      "The TWUSRK EOS predicted liquid molar fraction is 0.04623410.\n",
      "The PRTranslatedConsistent EOS predicted liquid molar fraction is 0.05738467.\n",
      "The SRKTranslatedConsistent EOS predicted liquid molar fraction is 0.06963456.\n"
     ]
    }
   ],
   "source": [
    "for obj, obj_name in zip(flasher_objects, flasher_names):\n",
    "    state_1 = obj.flash(T=T1, P=P1, zs=zs)\n",
    "    state_2 = obj.flash(P=P2, H=state_1.H(), zs=zs)\n",
    "    print(f'The {obj_name} EOS predicted liquid molar fraction is {state_2.LF:.8f}.')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "refined-baptist",
   "metadata": {},
   "source": [
    "As can be see, the equation of state used changes the results drastically. Even the best of the cubic equations of state given results 30-50% off from the high-precision equation of state. This problem was admittedly constructed to show off the importance of using higher precision models, but the point applies elsewhere also."
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
